const api = require("../../utils/apiUtils");
const app = getApp()

Page({
  data: {
    title:app.globalData.title
  },

  // 4:设备类型  5：设备序列号
  _handlePath(path){
    if(path[4]=='cdcz' || path[4]=='cdb' || path[4]=='pcl' || path[4]=='bxscn'){
        console.log("设备号："+path[5]);
        if(path[4] == 'bxscn'){
            my.reLaunch({
                url: `/packageA/pages/bxscn/index/index?deviceSn=${path[5]}`
            })
        }else{
            my.reLaunch({
                url: `/packageA/pages/${path[4]}/index/index?deviceSn=${path[5]}`
            })
        }
    }else{
        my.showModal({
            title:'二维码错误',
            content:'请重新扫描正确的二维码',
            showCancel:false,
            success(){
                wx.reLaunch({
                    url: "/pages/index/index",
                })
            }
        })
    }
  },

  toIndex(){
    my.reLaunch({
      url: '/pages/index/index',
    })
  },

  onLoad(options) {
    //获取
    let launchOptions = my.getLaunchOptionsSync();
    console.log(launchOptions)

    if(launchOptions.query && launchOptions.query.qrCode){
      let urls = launchOptions.query.qrCode.replace(/%2F/g,'/');
      let path = urls.split("/");
      console.log(urls);
      console.log(path);

      //登录逻辑
      if (!app.globalData.userInfo.loginToken) {
          api.login((res) => {
              this._handlePath(path)
          }, (res) => {
              this.toIndex();
          })
      }else{
          this._handlePath(path)
      }
    }else{
        this.toIndex();
        return;
    }
  }
});
